//
//  FourthViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "FourthViewController.h"
#import "loginViewController.h"
#import "collectActViewController.h"
#import "collectMovieViewController.h"
#import "DataBaseHandle.h"
#import "UserModel.h"

@interface FourthViewController ()<UITableViewDataSource, UITableViewDelegate, ItemDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIBarButtonItem *item;
@end

@implementation FourthViewController
// 实现协议方法
- (void)changeItem{
    self.navigationItem.rightBarButtonItem.title = @"注销";
    self.tabBarController.tabBar.hidden = NO;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的";
    self.item = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    // 每次开机先去数据库判断是否有用户在上次登录的时候仍处于登录状态，如果有，就让该用户仍处于登录状态
    UserModel *user = [[DataBaseHandle sharedDataBaseHandle] selectUserState:@"是"];
    if ([user.userName isEqualToString:@""]) {
        self.navigationItem.rightBarButtonItem = _item;
        [_item release];
    } else {
        self.navigationItem.rightBarButtonItem = _item;
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }
   
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.arr = [NSMutableArray arrayWithObjects:@"我的活动", @"我的电影", @"清除缓存", nil];
    
    
}

// 点击右上角登录方法
- (void)itemAction:(UIBarButtonItem *)item {
    loginViewController *loginVC = [[loginViewController alloc] init];
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"登录"]) {
        // 指定代理人
        loginVC.delegate = self;
  //*****************************************************************************************
        // 推过去以后tabBar隐藏（再pop过来以后就不会隐藏）
        loginVC.hidesBottomBarWhenPushed = YES;
// ********************************************************************************************
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"注销"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            //将数据库中已登录的用户的状态修改为否
            UserModel *model = [[DataBaseHandle sharedDataBaseHandle] selectUserState:@"是"];
            [[DataBaseHandle sharedDataBaseHandle] updateTable:model];
            
            self.navigationItem.rightBarButtonItem.title = @"登录";
            UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:nil message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
            // 提示注销成功
            [self presentViewController:alert1 animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action2];
        [alert addAction:action1];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }
  
    // 哈哈哈
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = _arr[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
// 点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"注销"]) {
        // 清除缓存
        if (indexPath.row == 2) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认清除" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:nil message:@"已清除" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert1 animated:YES completion:nil];
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }];
            [alert addAction:action1];
            [alert addAction:action2];
            [self presentViewController:alert animated:YES completion:nil];
        }
        // 我的活动
        if (indexPath.row == 0) {
            collectActViewController *ActVC = [[collectActViewController alloc] init];
            [self.navigationController pushViewController:ActVC animated:YES];
            [ActVC release];
        }
        // 我的电影
        if (indexPath.row == 1) {
            collectMovieViewController *movieVC = [[collectMovieViewController alloc] init];
            [self.navigationController pushViewController:movieVC animated:YES];
            [movieVC release];
        }

    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
